<%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@page contentType="text/xml"%>
<%@page pageEncoding="UTF-8"%>
<%
        response.setHeader("Cache-Control", "no-cache"); //HTTP 1.1
        response.setHeader("Pragma", "no-cache"); //HTTP 1.0
        response.setDateHeader("Expires", 0); //prevent caching at the proxy server
%>
<sql:query var="query" dataSource="jdbc/dbo_MicrosoftSQLServer">
    select a.id_modulo,a.id_componente, a.id_alocacao,
    a.data_alocacao,
    hora_inicio,
    hora_fim ,   
    s.desc_sala,
    u.desc_unidade,   
    a.situacao,
    a.subturma,
    a.otimizado,
    a.matricula,
    d.nome
    from Alocacoes a
    inner join Turmas  t on a.id_turma=t.id_turma
    inner join turnos h on t.id_turno=h.id_turno
    left join salas s on a.id_sala=s.id_sala
    left join unidades u on s.id_unidade=u.id_unidade
    left join docentes d on d.matricula=a.matricula
    where exists(select 1 from reservas r where a.id_turma=r.id_turma
    and a.id_componente=r.id_componente  and r.id_reserva=? and a.subturma=r.subturma)
    <sql:param value="${param.id}"/>
    order by data_alocacao
</sql:query>

<rows>
    <c:choose>
        <c:when test="${empty param.page}">
            <page>1</page>
        </c:when>
        <c:otherwise>
            <page>${param.page}</page>
        </c:otherwise>
    </c:choose>
    <total><fmt:formatNumber value="${query.rowCount}" pattern="######"/></total>
    <records>${query.rowCount} </records>
    <c:forEach var="row" items="${query.rows}">
        <row id ="${row.id_alocacao}">
            <cell><fmt:formatDate pattern="dd/MM/yyyy" value="${row.data_alocacao}"/></cell>
            <cell>${fn:trim(row.hora_inicio)}</cell>
            <cell>${fn:trim(row.hora_fim)}</cell>
            <cell>${fn:trim(row.desc_sala)} - ${fn:trim(row.desc_unidade)}</cell>
            <cell>${fn:trim(row.subturma)}</cell>
            <cell><![CDATA[${fn:trim(row.situacao)}]]></cell>
            <cell><![CDATA[${row.otimizado}]]></cell>
            <cell><![CDATA[${row.matricula}-${row.nome}]]></cell>
        </row>
    </c:forEach>
</rows>
